<template>
  <div class="search-history">
    <van-cell title="历史记录">
      <template v-if="isDeleteShow">
        <span @click="$emit('delete-history')">全部删除</span>
        &nbsp;
        <span @click="isDeleteShow = false">完成</span>
      </template>
      <van-icon v-else @click="isDeleteShow = true" name="delete-o" slot="default"></van-icon>
    </van-cell>
    <van-cell @click="historyClick(index, item)" :title="item" v-for="(item, index) in searchHistorys" :key="index">
      <van-icon v-if="isDeleteShow" name="close" slot="default"></van-icon>
    </van-cell>
  </div>
</template>

<script>
// import { getSearchHistory } from '@/api/search'
export default {
  name: 'SearchHistory',
  props: {
    searchHistorys: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      isDeleteShow: false // 控制删除功能的显示隐藏
    }
  },
  methods: {
    // 历史记录点击事件
    historyClick (index, item) {
      // 如果是删除状态就是删除
      if (this.isDeleteShow) {
        this.searchHistorys.splice(index, 1)
      } else {
        this.$emit('search', item)
      }
    }
  }
}
</script>

<style scoped lang="less">
</style>
